<?php

class Search {
	private $message = "" ;
	private $page = 1 ;
	private $limit = 10 ;
	private $count = 0 ;
	private $allPage = 0 ;
    function Search() {
    	
    }
    
    public function run() {
    	if (preg_match('/^@(\S+)\s*(.*)$/is', trim($_POST[Message]), $m)) {
			$funName = $m[1];
			if(method_exists($this,$funName))
			{
				$this-> $funName($m);
			}
		}
    	
    }
    
    private function my($m) {
    	$p = '/([0-9]+)/is';
    	if(preg_match($p,$m[2],$n)) {
    		$this->page = $n[1] ;
    	}
    	$sql = "select count(*) A from td_study where flag =1 and qq='{$_POST[Sender]}' ";
		$ret = DB :: GetQueryResult($sql, false);
		$this->count = $ret[0][a];
		$this->allPage = ceil($this->count / $this->limit);
		$this->page = $this->page>$this->allPage?$this->allPage:$this->page ;
		$condition = array (
			'qq' => $_POST[Sender],
			'flag'=>1		
		);

		$options = array (
			'condition' => $condition,
			'offset' => ($this->page -1) * $this->limit,
			'size' => $this->limit,
			'order' => 'order by id DESC '
		);
		$ret = DB :: LimitQuery('td_study', $options);
		$i = 1 ;
		foreach ($ret as $a) {
			$clusterName = getClusterNameBynum($a[clusternum]) ;
			if($a[clusternum]==$_POST[ClusterNum]  ) {
				$clusterName = $clusterName."(". $a[clusternum].")" ;
			} 
			$this->message = $this->message.$i++ .".{$_POST['Nick']}于".$a[update_time]."在群{$clusterName}中\n  学习：".$a[question]."\n  回答：".$a[answer]."\n" ;
		}
		$this->message = $this->message.$this->getPageBbar($m) ;
		
    }
    
    private function getPageBbar($m) {
    	if ($this->count > 0) {
			$s  =  "首  页  -> " . "@{$m[1]} 1\n";
			if ($this->page != 1 && $this->page-1 != 1)
				$s = $s . "上一页-> " . "@{$m[1]} " . ($this->page -1) . "\n";
			if ($this->page != $this->allPage)
				$s = $s . "下一页-> " . "@{$m[1]} " . ($this->page +1) . "\n";
			$s = $s. "尾  页  -> " . "@{$m[1]} ".$this->allPage ;
			return $s ;
		}
    }
    
    private function __get($property_name) {
		return $this-> $property_name;
	}

	private function __set($property_name, $value) {
		$this->$property_name = $value;
	}  
}
?>